### Re-examining Civil War: Ethnicity, Horizontal Inequality, and Space
### Grant Gordon & Evann Smith

library(foreign)
library(Amelia)

gsdata <- read.csv("gsdataFINAL.csv")

#order data by year
gsdata <- gsdata[with(gsdata, order(year)),]

#transfrom relpop so bounded [0, inf]
sqrelpop <- sqrt(gsdata$relpop)
gsdata <- cbind(gsdata, sqrelpop)

#bound conc
bound <- matrix(data=NA, ncol=3, nrow=1)
bound[1,1] <- 45
bound[1,2] <- 0
bound[1,3] <- 1

#impute data
impdata1 <- amelia(x=gsdata, cs="ccode", ts="year", noms=c("warl", "Oil", "ncontig", "instab", "deml", "anocl", "discrim", "sepautonomy", "regautonomy", "govpower2", "powerless"), ords=c("govpower", "polity2", "polity2l"), idvars=c("country", "cname", "casename", "waryrs", "region", "western", "eeurop", "lamerica", "ssafrica", "asia", "relpop"), bounds=bound, intercs=TRUE, m=5)

#re-transform relpop
impdata1[[1]][[1]]$relpop <- impdata[[1]][[1]]$sqrelpop^2
impdata1[[1]][[2]]$relpop <- impdata[[1]][[2]]$sqrelpop^2
impdata1[[1]][[3]]$relpop <- impdata[[1]][[3]]$sqrelpop^2
impdata1[[1]][[4]]$relpop <- impdata[[1]][[4]]$sqrelpop^2
impdata1[[1]][[5]]$relpop <- impdata[[1]][[5]]$sqrelpop^2

plot(impdata)

save(impdata1, file="imp1.RData")